import Router from 'koa-router';
import ResponseOk from '../../utils/ResponseOk';
import DPGroupService from '../../service/DPGroupService';
import { parsePage, pgyColumns, qianguaColumns } from '../../utils/common';
import { Context } from 'koa';
import { Rules } from 'async-validator';
import validate from '../../utils/validate';
import { isEmpty, isNull } from 'lodash-es';
import PgyService from '../../service/PgyService';

const router = new Router();

router.get('/init_columns', async (ctx) => {
  const data = { qianguaColumns: qianguaColumns(), pgyColumns: pgyColumns() };
  ctx.body = ResponseOk.success(data);
});

router.post('/dp_group/getlist', async (ctx) => {
  const { page, pageSize } = parsePage(ctx);
  const data = await DPGroupService.getList(page, pageSize);
  ctx.body = ResponseOk.success(data);
});
router.post('/dp_group/addtopic', async (ctx: Context) => {
  const rules: Rules = {
    title: [{ type: 'string', required: true, message: '任务名称不能为空' }],
  };

  const { error } = await validate(ctx, rules);
  if (!isNull(error)) {
    throw new global.errs.ParameterException(error, 400);
  }
  const post = ctx.request.body;
  const data = {
    title: post['title'],
    type: post['type'],
  };
  if (post['type'] == 2) {
    data['condition'] = post['condition'];
  }
  if (post['type'] == 1) {
    data['pgy_data'] = post['pgy_data'];
  }
  await DPGroupService.addTopic(data);
  ctx.body = ResponseOk.success();
});
router.post('/dp_group/checkcolumns', async (ctx: Context) => {
  const rules: Rules = {
    id: [{ type: 'number', required: true, message: 'ID编号不能为空' }],
    columns: [{ type: 'array', required: true, message: '导出列不能为空' }],
  };

  const { error, data } = await validate(ctx, rules);
  if (!isNull(error)) {
    throw new global.errs.ParameterException(error, 400);
  }
  await DPGroupService.updateColumns(data);
  ctx.body = ResponseOk.success();
});
router.get(`/dp_group/startfetch/:id`, async (ctx: Context) => {
  const id = ctx.params.id;

  if (!id) {
    throw new global.errs.ParameterException('参数有误', 400);
  }
  await DPGroupService.startFetch(id);
  ctx.body = ResponseOk.success();
});
router.get('/db_group/fineone/:id', async (ctx: Context) => {
  const id = ctx.params.id;
  if (!id) {
    throw new global.errs.ParameterException('参数有误', 400);
  }
  const row = await DPGroupService.fineOne(id);
  ctx.body = ResponseOk.success(row);
});

router.post('/dp_group/dplist', async (ctx: Context) => {
  const { page, pageSize } = parsePage(ctx);
  const { groupId } = ctx.request.body;
  if (isEmpty(groupId)) {
    throw new global.errs.ParameterException('参数有误', 400);
  }
  const data = await PgyService.getList(page, pageSize, { groupId: groupId });
  ctx.body = ResponseOk.success(data);
});
router.post('/dp_group/updatefield/:id', async (ctx: Context) => {
  const id = ctx.params.id;
  if (!id) {
    throw new global.errs.ParameterException('参数有误', 400);
  }
  const rules: Rules = {
    gid: [{ type: 'number', required: true, message: 'ID编号不能为空' }],
    key: [{ type: 'string', required: true, message: '字段名称不能为空' }],
    value: [{ type: 'string', required: true, message: '字段值不能为空' }],
  };

  const { error } = await validate(ctx, rules);
  if (!isNull(error)) {
    throw new global.errs.ParameterException(error, 400);
  }
});
export default router;
